using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class xPlayer : System.Web.UI.UserControl, System.Web.UI.ICallbackEventHandler 
{
    protected string returnValue = "default";
    public string sCallBackFunctionInvocation;

    protected void Page_Load(object sender, EventArgs e)
    {
        sCallBackFunctionInvocation = this.Page.ClientScript.GetCallbackEventReference(this, "message", "showClipDetail", "context", "OnError", false);
    }


    public void RaiseCallbackEvent(String eventArgument)
    {
        DataSet dsClipDetail = this.getClipDetailFromDB(eventArgument);
        StringBuilder strDivHTML = new StringBuilder();

        if (dsClipDetail.Tables[0].Rows.Count > 0)
        {
            strDivHTML.Append("<table class='vidTableDetail'>");
            strDivHTML.Append("<tr>");
            strDivHTML.Append("<td>");
            strDivHTML.Append(string.Format("<span class='vidTableDetailLabel'>Details: <span class='vidTableDetailLabelInfo'>{0}</span></span>", dsClipDetail.Tables[0].Rows[0]["details"].ToString()));
            strDivHTML.Append("</td>");
            strDivHTML.Append("</tr>");
            strDivHTML.Append("</table>");
        }
        else
            strDivHTML.Append("<span>details not available for media.</span>");

        returnValue = strDivHTML.ToString();
    }

    public string GetCallbackResult()
    {
        return returnValue;
    }

    protected DataSet getClipDetailFromDB(string ID)
    {
        DataSet tmpDs = new DataSet();
        DataSet tmpDsVid = new DataSet();

        tmpDsVid.Tables.Add("MediaByID");
        tmpDsVid.Tables[0].Columns.Add("id");
        tmpDsVid.Tables[0].Columns.Add("path");
        tmpDsVid.Tables[0].Columns.Add("details");

        try
        {
            tmpDs.ReadXml(Server.MapPath("~/xLitePlayer_data/xMediaPaths.xml"));

            tmpDsVid = tmpDs.Clone();

            for (int i = 0; i < tmpDs.Tables[0].Rows.Count; i++)
            {
                if (tmpDs.Tables[0].Rows[i]["id"].ToString() == ID)
                {
                    DataRow dr = tmpDsVid.Tables[0].NewRow();

                    dr["details"] = tmpDs.Tables[0].Rows[i]["details"].ToString();

                    tmpDsVid.Tables[0].Rows.Add(dr);

                    break;
                }
            }

        }
        catch (Exception)
        {
            //TODO throw error;   
        }

        return tmpDsVid;
    }
}
